package com.ddd.wandemo;

import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v7.app.AppCompatDelegate;
import android.view.MenuItem;
import android.widget.LinearLayout;

import com.base.NormalBaseActivity;
import com.constant.Constant;
import com.event.ThemeEvent;
import com.ui.home.view.HomeFragment;
import com.ui.knowledge.KnowledgeFragment;
import com.ui.mine.view.MineFragment;
import com.ui.other.OtherFragment;
import com.ui.project.ProjectFragment;
import com.util.SpUtil;
import com.widget.BottomNavigationViewHelper;
import com.widget.CustomViewPager;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends NormalBaseActivity {

    private static final String tag = "WAN";
    private BottomNavigationView mBnvHome;
    private CustomViewPager mVpMain;
    private List<Fragment> mFragments = new ArrayList<>();
    private LinearLayout mLlRoot;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_main;
    }


    @Override
    protected void initView() {
        mBnvHome = findViewById(R.id.bnv_home);
        mVpMain = findViewById(R.id.vp_main);
        mLlRoot = findViewById(R.id.ll_root);
        BottomNavigationViewHelper.disableShiftMode(mBnvHome);

        mFragments.add(HomeFragment.newInstance());
        mFragments.add(KnowledgeFragment.newInstance());
        mFragments.add(ProjectFragment.newInstance());
        mFragments.add(new OtherFragment());
        mFragments.add(new MineFragment());

        HomeVpAdapter homeVpAdapter = new HomeVpAdapter(getSupportFragmentManager());
        mVpMain.setAdapter(homeVpAdapter);
        mVpMain.setCurrentItem(0);
        mVpMain.setOffscreenPageLimit(mFragments.size());
    }


    @Override
    protected void initEvent() {

        mBnvHome.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.action_home:
                        mVpMain.setCurrentItem(0);
                        return true;
                    case R.id.action_system:
                        mVpMain.setCurrentItem(1);
                        return true;
                    case R.id.action_guide:
                        mVpMain.setCurrentItem(2);
                        return true;
                    case R.id.action_sort:
                        mVpMain.setCurrentItem(3);
                        return true;
                    case R.id.action_mine:
                        mVpMain.setCurrentItem(4);
                        return true;
                }
                return false;
            }
        });
    }


    public class HomeVpAdapter extends FragmentPagerAdapter {
        public HomeVpAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments == null ? 0 : mFragments.size();
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void handleThemeState(ThemeEvent themeEvent) {
        if (themeEvent.isNight()) {
            AppCompatDelegate.setDefaultNightMode(
                    AppCompatDelegate.MODE_NIGHT_YES);
        } else {
            AppCompatDelegate.setDefaultNightMode(
                    AppCompatDelegate.MODE_NIGHT_NO);
        }
        SpUtil.setBoolean(Constant.IS_NIGHT,themeEvent.isNight());
        recreate();
    }


    @Override
    public void onStart() {
        super.onStart();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

    /**
     * 展示一个切换动画
     */
    /*public void showAnimation() {
        final View decorView = getWindow().getDecorView();
        Bitmap cacheBitmap = getCacheBitmapFromView(decorView);
        if (decorView instanceof ViewGroup && cacheBitmap != null) {
            final View view = new View(this);
            view.setBackgroundDrawable(new BitmapDrawable(getResources(), cacheBitmap));
            ViewGroup.LayoutParams layoutParam = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT);
            ((ViewGroup) decorView).addView(view, layoutParam);
            ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(view, "alpha", 1f, 0f);
            objectAnimator.setDuration(3000);
            objectAnimator.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    super.onAnimationEnd(animation);
                    ((ViewGroup) decorView).removeView(view);
                }
            });
            objectAnimator.start();
        }
    }*/

    /**
     * 获取一个 View 的缓存视图
     *
     * @param view
     * @return
     */
    /*private Bitmap getCacheBitmapFromView(View view) {
        final boolean drawingCacheEnabled = true;
        view.setDrawingCacheEnabled(drawingCacheEnabled);
        view.buildDrawingCache(drawingCacheEnabled);
        final Bitmap drawingCache = view.getDrawingCache();
        Bitmap bitmap;
        if (drawingCache != null) {
            bitmap = Bitmap.createBitmap(drawingCache);
            view.setDrawingCacheEnabled(false);
        } else {
            bitmap = null;
        }
        return bitmap;
    }*/
}



